package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.adapter.CarfromTwoAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.LocalGroupSearch;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.Bind;
import butterknife.OnClick;
import master_bean.CarForBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;


/**
 * Created by yidong on 2017/5/23.
 */

public class MarketActivity extends BaseActivity {
    @Bind(R.id.recycler_view)
    RecyclerView carfromTwoRecycler;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.ed_text)
    EditText searchEd;
    private CarfromTwoAdapter twoAdapter;
    private List<CarForBean> carForBean = new ArrayList<>();
    private String marketName;
    private Intent intent;
    private String marketId;
    private LinearLayoutManager twoManager;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        intent = new Intent();
        titleTv.setText("选择车市");
        type = getIntent().getIntExtra("TYPE", 1);
    }

    private void initViews() {
        twoAdapter = new CarfromTwoAdapter(this, R.layout.layout_item_carfrom_two, carForBean, CarfromTwoAdapter.TYPE_TWO);
        twoManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        carfromTwoRecycler.setLayoutManager(twoManager);
        carfromTwoRecycler.setAdapter(twoAdapter);
        twoAdapter.setOnItemClickListener(twoListener);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_dealers_list;
    }

    @Override
    protected void initEventAndData() {
        searchEd.addTextChangedListener(textWatcher);
    }

    @Override
    protected void createPresenter() {
        requestCarMarket(this);
    }

    private MultiItemTypeAdapter.OnItemClickListener twoListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            marketName = twoAdapter.getDatas().get(position).getMarket_name();
            marketId = twoAdapter.getDatas().get(position).getMarket_id();
            intent.putExtra("marketName", marketName);
            intent.putExtra("marketId", marketId);
            switch (type) {
                case 1://只显示车市
                    setResult(RequestAndResultCode.SelectColorActivity_ResultCode, intent);
                    finish();
                    break;
                case 2://跳转到车行选择界面
                    intent.setClass(MarketActivity.this, CarFromActivity.class);
                    intent.putExtra("TYPE", 1);
                    startIntent(intent, 2);
                    break;
                case 3://跳转到车行选择界面
                    intent.setClass(MarketActivity.this, CarFromActivity.class);
                    intent.putExtra("TYPE", 2);
                    startIntent(intent, 3);
                    break;
                case 4:
                    intent.setClass(MarketActivity.this, EditDealerActivity.class);//修改車行
                    intent.putExtra("CAR_ID",getIntent().getStringExtra("CAR_ID"));
                    startIntent(intent, 4);
                    break;
            }
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };

    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 3) {
            Intent intent = new Intent();
            intent.putExtra("carForName", data.getStringExtra("carForName"));
            intent.putExtra("carFofId", data.getStringExtra("carFofId"));
            setResult(RequestAndResultCode.CarFromActivity_ResultCode, intent);
            finish();
        }
        if (resultCode == 2){
            setResult(2);
            finish();
        }
    }

    public void requestCarMarket(Context context) {
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, new MapUtil().build()).create(IApiActivityRequest.class).getMarket(),
                new BaseSubscriber<TokenBean<List<CarForBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<CarForBean>> o) {
                        carForBean = o.getData();
                        initViews();
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
            if (s != null && s.length() > 0) {
                List<CarForBean> listG = LocalGroupSearch.searchGroup(s, (ArrayList<CarForBean>) carForBean);
                if (listG != null && listG.size() > 0) {
                    if (twoAdapter != null)
                        twoAdapter.refresh(listG);
                } else {
                    showToast("搜索为空");
                }

            } else {
                if (twoAdapter != null)
                    twoAdapter.refresh(carForBean);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    };
}
